package com.example.androidproject.utils;

import android.os.Bundle;
import android.os.Message;
import android.util.Log;

import androidx.annotation.NonNull;

import com.example.androidproject.app.classifybean.ClassifyBean;
import com.example.androidproject.movie.bean.ResultBeanData;
import com.example.androidproject.novel.bean.NovelBean;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.CountDownLatch;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class GetContent {
    private static String TAG = "GGSTUDY";
    public static int ClassifyFlag;
    public static RvBottomManager rvBottomManager;

    public static void getContent(String classify, int classifyFlag, int limit, int page, CountDownLatch latch) {
        ClassifyFlag = classifyFlag;
        OkHttpClient okHttpClient = new OkHttpClient();
        String url = "";
        if (classifyFlag == 1) {
            url = RequestConstant.MOVIE_IMG_URL + "?" + "classify=" + classify + "&" + "limit=" + limit + "&" + "page=" + page;
        }
        if (classifyFlag == 2) {
            url = RequestConstant.NOVEL_URL + "?" + "classify=" + classify + "&" + "limit=" + limit + "&" + "page=" + page;
        }
        Log.d(TAG, "getContent: " + url);
        Request request = new Request.Builder().url(url).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                Log.d(TAG, "onFailure: " + e);
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                String result = response.body().string();
                copeData(result, 2);
                Log.d(TAG, "onResponse: 123");
//                ResultsConstant.requsetOver = true;
                latch.countDown();
            }
        });
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    //    private void getOptions() {
//        String classify = "电影";
//        if (classifyFlag == 1) {
//            classify = "电影";
//        }
//        if (classifyFlag == 2) {
//            classify = "小说";
//        }
//        String url = RequestConstant.CLASSIFY_URL + "?" + "classify=" + classify;
//        Request request = new Request.Builder().url(url).build();
//        Call call = okHttpClient.newCall(request);
//        call.enqueue(new Callback() {
//            @Override
//            public void onFailure(@NonNull Call call, @NonNull IOException e) {
//                Log.d(TAG, "onFailure: " + e);
//            }
//
//            @Override
//            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
//                if (response.isSuccessful()) {
//                    GetContent.copeData(response.body().string(), 1);
//                    latch.countDown();
//                }
//            }
//        });
//    }

    public static void copeData(String json, int getflag) {

        Gson gson = new Gson();
        ClassifyBean classifyBean = gson.fromJson(json, ClassifyBean.class);

        if (classifyBean.getStatus() == 0) {
            Message message = new Message();
            Bundle bundle = new Bundle();
            message.setData(bundle);
            //
            if (getflag == 1) {
                // 获取分类导航栏
                ResultsConstant.OptionData = (ArrayList<ClassifyBean.DataBean>) classifyBean.getData();
            }
            if (getflag == 2) {
                // 获取内容
                if (ClassifyFlag == 1) {
                    ResultBeanData resultBeanData = gson.fromJson(json, ResultBeanData.class);
                    ResultsConstant.contentMovie = (ArrayList<ResultBeanData.DataBean>) resultBeanData.getData();
                    ResultsConstant.MovieList.addAll(ResultsConstant.contentMovie);
                    ResultsConstant.total = resultBeanData.getTotal();
                }
                if (ClassifyFlag == 2) {
                    NovelBean novelBean = gson.fromJson(json, NovelBean.class);
                    ResultsConstant.contentNovel = (ArrayList<NovelBean.DataBean>) novelBean.getData();
                    ResultsConstant.NovelList.addAll(ResultsConstant.contentNovel);
                    ResultsConstant.total = novelBean.getTotal();
                }
            }
            ResultsConstant.isLoading = false;
        } else {
            Log.d(TAG, "请求失败");
        }
    }
}
